﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;

namespace BaiDuSitemap.DBO
{
    public class Group
    {
        /// <summary>
        /// 获取分组列表
        /// </summary>
        /// <returns>DataTable</returns>
        public static DataTable getAll()
        {
            string sql = "select [gid],[gname],[comment] from [QT_Group] order by [gname] asc";
            DataTable dt = new QT.DBO.OleDBHelper(myDB.connstring).DataTable(sql);
            return dt;
        }

        /// <summary>
        /// 添加分组。返回值-2为已存在，-1为失败，>=0为操作记录条数
        /// </summary>
        /// <param name="gname">分组名称</param>
        /// <param name="comment">备注</param>
        /// <returns>int</returns>
        public static int Add(string gname, string comment)
        {
            if (IsExist(gname, 0)) return -2;

            string sql = "insert into [QT_Group]([gname],[comment]) values('" + gname + "','" + comment + "')";
            return new QT.DBO.OleDBHelper(myDB.connstring).ExecuteNonQuery(sql);
        }

        /// <summary>
        /// 修改分组信息。返回值-2为分组名称已存在，-1为失败，>=0为操作记录条数
        /// </summary>
        /// <param name="gname">旧分组名称</param>
        /// <param name="newgname">新分组名称</param>
        /// <param name="comment">备注</param>
        /// <returns>int</returns>
        public static int Modify(string gname,string newgname, string comment)
        {
            if ((gname != newgname) && IsExist(newgname, 0)) return -2;

            string sql = "update [QT_Group] set [gname]='" + newgname + "',[comment]='" + comment + "' where [gname]='" + gname + "'";
            return new QT.DBO.OleDBHelper(myDB.connstring).ExecuteNonQuery(sql);
        }

        /// <summary>
        /// 修改分组信息
        /// </summary>
        /// <param name="gid">分组ID</param>
        /// <param name="gname">分组名称</param>
        /// <param name="comment">备注</param>
        /// <returns>int</returns>
        public static int Modify(int gid, string gname, string comment)
        {
            if (IsExist(gname, gid)) return -2;

            string sql = "update [QT_Group] set [gname]='" + gname + "',[comment]='" + comment + "' where [gid]<>" + gid;
            return new QT.DBO.OleDBHelper(myDB.connstring).ExecuteNonQuery(sql);
        }

        /// <summary>
        /// 删除指定分组。返回值-2为不存在，-1为失败，>=0为操作记录条数
        /// </summary>
        /// <param name="gname">分组名称</param>
        /// <returns>int</returns>
        public static int Delete(string gname)
        {
            if (!IsExist(gname, 0)) return -2;

            string sql = "delete from [QT_Group] where [gname]='" + gname + "'";
            return new QT.DBO.OleDBHelper(myDB.connstring).ExecuteNonQuery(sql);
        }

        /// <summary>
        /// 删除一组分组。返回值-1为失败，>=0为操作记录条数
        /// </summary>
        /// <param name="gnames"></param>
        /// <returns></returns>
        public static int Delete(List<string> gnames)
        {
            string sql = "delete from [QT_Group] where [gname] in('" + string.Join("','", gnames.ToArray()) + "')";
            return new QT.DBO.OleDBHelper(myDB.connstring).ExecuteNonQuery(sql);
        }

        /// <summary>
        /// 检查分组名称是否已存在
        /// </summary>
        /// <param name="gname">分组名称</param>
        /// <returns>bool</returns>
        public static bool IsExist(string gname,int gid)
        {
            string sql = "select [gid] from [QT_Group] where [gname]='" + gname + "'" + (gid == 0 ? "" : (" and [gid]=" + gid));
            DataTable dt = new QT.DBO.OleDBHelper(myDB.connstring).DataTable(sql);
            if (dt.Rows.Count > 0) return true;
            else return false;
        }
    }
}
